﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KornyezetVedelem;
using System.Runtime.InteropServices;
namespace Formok
{
    public partial class Ideiglenes_Form : Form
    {
        const int MF_BYPOSITION = 0x400;
        [DllImport("User32")]
        private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
        [DllImport("User32")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("User32")]
        private static extern int GetMenuItemCount(IntPtr hWnd);
        private Ideiglenes ob;
        private String str;
        private KorVedelem korVedelem;
        private int melyik;
        public Ideiglenes_Form(KorVedelem korVedelem)
        {
            this.korVedelem = korVedelem;
            InitializeComponent();
        }
        public void Aktival()
        {
            IntPtr hMenu = GetSystemMenu(this.Handle, false);
            int menuItemCount = GetMenuItemCount(hMenu);                //a kilepes menu kiveve
            RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
            comboBox1.Enabled = true;
            Nev.Enabled = true;
            Megse.Enabled = true;
            Mentes.Enabled = true;
        }
        public void DezAktival()
        {
            comboBox1.Enabled = false;
            Nev.Enabled = false;
            Mentes.Enabled = false;
            Megse.Enabled = false;
        }
        private void Ideiglenes_Form_Load(object sender, EventArgs e)
        {
            bool er = false;
            ob = new Ideiglenes(ref er);
            ob.feltoltCombo(comboBox1);
            DataSet dS = ob.IdeiglenesToltDS(ref str);
            dataGridView1.DataSource = dS.Tables["Tablak"];
            DezAktival();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            korVedelem.Show();
        }

        private void Kilpes_button_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Beszuras_Click(object sender, EventArgs e)
        {
            Aktival();
            melyik = 1;
        }

        private void Torles_Click(object sender, EventArgs e)
        {
            DezAktival();
            if (dataGridView1.CurrentRow != null)
            {
                if (MessageBox.Show("Biztos vagy benne h torolni akarod ezt a  sort?\n", "Torles", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    String id = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    Exception ex1 = ob.Torol(id);
                    if (ex1 != null)
                        MessageBox.Show("Nem Tudtam Torolni ezt a sort!\n\n" + Convert.ToString(ex1));
                    else
                    {

                        DataSet dS = ob.IdeiglenesToltDS(ref str);
                        dataGridView1.DataSource = dS.Tables["Tablak"];
                        MessageBox.Show("Sikeresen torolve...");
                    }
                }
            }
            else
                MessageBox.Show("Nincs torlendo sor az adatbazisban");
        }

        private void Modositas_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow != null)
            {
                Aktival();
                melyik = 2;
            }
            else
                MessageBox.Show("Nincs Modositando sor az adatbazisban!");
        }

        private void Megse_Click(object sender, EventArgs e)
        {
            DezAktival();
        }

        private void Mentes_Click(object sender, EventArgs e)
        {
            if (melyik == 1)
            {
                String ellenkoz;
                String nev;
                if (comboBox1.SelectedItem == null)
                    MessageBox.Show("Valasszon ki egy Ellenorzokozpontot!");
                else
                {
                    ellenkoz = comboBox1.SelectedItem.ToString();
                    if (Nev.Text == "")
                        MessageBox.Show("Kerem irja be a nevet az Ideiglenes lerakat nevenek");
                    else
                    {
                        nev = Nev.Text;
                        ob.Beszur(nev, ellenkoz);
                        DataSet dS = ob.IdeiglenesToltDS(ref str);
                        dataGridView1.DataSource = dS.Tables["Tablak"];
                        DezAktival();
                        MessageBox.Show("Sikeresen Beszurva");
                    }
                }
            }
            else
                if (melyik == 2)
                {
                    String ellenkoz;
                    String nev;
                    String id = dataGridView1.CurrentRow.Cells[0].Value.ToString();

                    if (comboBox1.SelectedItem == null)
                        MessageBox.Show("Valasszon ki egy Ellenorzokozpontot!");
                    else
                    {
                        ellenkoz = comboBox1.SelectedItem.ToString();

                        if (Nev.Text == "")
                            MessageBox.Show("Kerem irja be a nevet az ideiglenes lerakatnak");
                        else
                        {
                            nev = Nev.Text;
                            ob.Modosit(id, nev, ellenkoz);
                            DataSet dS = ob.IdeiglenesToltDS(ref str);
                            dataGridView1.DataSource = dS.Tables["Tablak"];
                            DezAktival();
                            MessageBox.Show("Sikeresen Modositva");
                        }
                    }
                }
        }
    }
}
